/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package race.util;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import race.component.RaceCanvas;

/**
 *
 * @author BBXP
 */
public class Tool {
    private static final String DB_NAME = "GameRecord";
    private static RecordStore rs;

    public static void openMyRecord(){
        try {
            rs = RecordStore.openRecordStore(DB_NAME, true);
            if(rs.getNumRecords() == 0){
                byte[] s = (Integer.MAX_VALUE+"").getBytes();
                for(int i = 0; i < RaceCanvas.HARD; i++){
                    rs.addRecord(s, 0, s.length);
                }
            }
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    public static int updateRecord(int level, int value){
        int result = -1;
        try {
            byte[] b = rs.getRecord(level);
            int oldRecord = Integer.parseInt(new String(b));
            if(value < oldRecord){
                result = -1;
                byte[] newRecord = (value+"").getBytes();
                rs.setRecord(level, newRecord, 0, newRecord.length);
            }else{
                result = oldRecord;
            }
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        return result;
    }

    public static int viewRecord(int level) throws Exception{
        int a = Integer.parseInt(new String(rs.getRecord(level)));
        if(a == Integer.MAX_VALUE){
            throw new Exception("Not initalized");
        }else{
            return a;
        }
    }

    public static void printAllRecords(){
        RecordEnumeration e;
        try {
            e = rs.enumerateRecords(null, null, true);
            while(e.hasNextElement()){
                try {
                    System.out.println(Integer.parseInt(new String(e.nextRecord())));
                } catch (RecordStoreException ex) {
                    ex.printStackTrace();
                }
            }
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }
    }

    public static void printRecord(int id){
        try {
            byte[] b = rs.getRecord(id);
            System.out.println(Integer.parseInt(new String(b)));
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }
    public static void closeRecord(){
        try {
            rs.closeRecordStore();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    public static String getInstruction(){
        return "Play:\n\n"
                + "Use arrow key to play\n\n"
                + "    Up accelerate\n"
                + "    DOWN decelerate\n"
                + "    LEFT/RIGHT steering\n\n\n"
                + "Hints\n\n"
                + "    1 passing a water pool decrease\n"
                + "      your speed\n"
                + "      (if you are in slow speed, water\n"
                + "       pool doesn't affect your speed)\n\n"
                + "    2 pass a back arrow sign can\n"
                + "      increase your speed to the max\n\n"
                + "    3 collide with opponent decrease\n"
                + "      both speed\n\n"
                + "    4 in normal difficuty or above,\n"
                + "      opponent turn into a obstructor\n\n"
                + "    5 higher the dificuty, harder to\n"
                + "      drive\n\n"
                + "© Wong Fu Hong & Chan";
    }
}
